<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
		
		<script src='../../../jsunit/app/jsUnitCore.js' type='text/javascript'></script>
		<script type="text/javascript" src='../javascript/magic_table.js'></script>
		
		<title>Matrix JSUnit tests</title>
	</head>
	<body>
		
		<script type='text/javascript'>
			
			var matrix;
			var rows = 10;
			var columns = 10;
			
			function createFullMatrixOfNumbers()
			{
				matrix = new greg.ross.visualisation.Matrix();
				
				for (var i = 0; i < rows; i++)
				for (var j = 0; j < columns; j++)
					matrix.put([i, j], (i*j));
			}
			
			function testFull2dMatrixOfNumbers()
			{
				createFullMatrixOfNumbers();
				assert(matrix.get([0, 0]) == 0);
				assert(matrix.get([9, 9]) == 81);
				assert(matrix.get([5, 4]) == 20);
			}
			
			function testSparse1dMatrix()
			{
				matrix = new greg.ross.visualisation.Matrix();
				matrix.put([1], 3);
				
				assert(matrix.get([1]) == 3);
				assert(matrix.get([0]) == null);
			}
			
			function testSparse2dMatrix()
			{
				matrix = new greg.ross.visualisation.Matrix();
				matrix.put([0, 0], 1);
				
				assert(matrix.get([0, 0]) == 1);
				assert(matrix.get([0, 1]) == null);
				assert(matrix.get([1, 1]) == null);
			}
			
			function testGetTooManyDimensions()
			{
				matrix = new greg.ross.visualisation.Matrix();
				matrix.put([1], 3);
				assert(matrix.get([0, 0]) == null);
			}
			
			function testSubSpacecPut()
			{
				matrix = new greg.ross.visualisation.Matrix();
				matrix.put([1], 3);
				matrix.put([0, 1], 2);
				assert(matrix.get([1]) == 3);
				assert(matrix.get([0, 1]) == 2);
				assert(matrix.get([1]) == 3);
				
				// Change put order.
				matrix = new greg.ross.visualisation.Matrix();
				matrix.put([0, 1], 2);
				matrix.put([1], 3);
				assert(matrix.get([1]) == 3);
				assert(matrix.get([0, 1]) == 2);
				assert(matrix.get([1]) == 3);
				
				matrix = new greg.ross.visualisation.Matrix();
				matrix.put([0], 2);
				assert(matrix.get([0]) == 2);
				assert(matrix.get([0, 0]) == 2);
				
				matrix = new greg.ross.visualisation.Matrix();
				matrix.put([1], 2);
				assert(matrix.get([1]) == 2);
				assert(matrix.get([1, 0]) == 2);
				
				// Test with 3 dimensions.
				matrix = new greg.ross.visualisation.Matrix();
				matrix.put([0], 2);
				assert(matrix.get([0]) == 2);
				assert(matrix.get([0, 0]) == 2);
				assert(matrix.get([0, 0, 0]) == 2);
				
				matrix = new greg.ross.visualisation.Matrix();
				matrix.put([1], 2);
				assert(matrix.get([1]) == 2);
				assert(matrix.get([1, 0]) == 2);
				assert(matrix.get([1, 0, 0]) == 2);
				assert(matrix.get([1, 0, 1]) == null);
			}
			
			function testConsecutivePutsOnSameDimension()
			{
				matrix = new greg.ross.visualisation.Matrix();
				matrix.put([0, 0], 3);
				matrix.put([0, 1], 2);
				assert(matrix.get([0, 0]) == 3);
				assert(matrix.get([0, 1]) == 2);
				
				matrix.put([1, 0], 4);
				matrix.put([1, 1], 5);
				assert(matrix.get([1, 0]) == 4);
				assert(matrix.get([1, 1]) == 5);
			}
			
			function testConsecutiveSubspacePutsOnSameDimension()
			{
				matrix = new greg.ross.visualisation.Matrix();
				matrix.put([0], 3);
				matrix.put([0, 1], 2);
				assert(matrix.get([0, 0]) == 3);
				assert(matrix.get([0, 1]) == 2);
				assert(matrix.get([0]) == '{0=3, 1=2}')
			}
		
		</script>
		
	</body>
</html>
